/**
 * 宽带预约列表
 */
(function($) {
  var pageData = {};
  var pageCount = 1;
  var filterType = 3;
  var pageNumber = 0;
  var opponentList = new Array();
  
  var idcData;
  /*
  封装参数
  */
  var getMyParams = function(id) {
    var params = "?";
    var myParam = getParams();
    if (myParam) {
      params += myParam;
      if (id) {
        params += "&";
      }
    }
    if (id) {
      params += "id=" + id;
    }

    return params;
  };

  var bind = function(object, clickFunc) {
    itemOnClick(object, clickFunc);
  }

  var itemOnClick = function(object, clickFunc) {
    $(object).off("click"); //取消li事件
    /*li事件*/
    $(object).on("click", function() {
      var $siblingsNode = $(this).siblings();
      var primaryClass = "select-item";
      var defaultColor = "#fff";
      var selectColor = "#0c79b1";

      if ($siblingsNode.hasClass(primaryClass)) {
        $siblingsNode.removeClass(primaryClass);
        $siblingsNode.css("background-color", defaultColor);
      }
      if ($(this).hasClass(primaryClass)) {
        $(this).removeClass(primaryClass);
        $(this).css("background-color", defaultColor);
      } else {
        $(this).addClass(primaryClass);
        $(this).css("background-color", selectColor);
      }
      clickFunc(this);
      event.stopPropagation();
    });
  }

  var  createObject =function (title,value){
　　var obj = new Object();
　　obj.title = title;
　　obj.value = value;
　　return obj;
　}

  /*
  显示数据
  */
  var showOpponentData = function(data) {
    $("#detail_title").html(data.bus_type+"对手信息");
    opponentList.push(createObject("集团客户名称", data.group_name));
    opponentList.push(createObject("运营商", data.operator));
    busType = data.bus_type;
    if(busType == "IDC"){
      opponentList.push(createObject("互联网出口带宽", data.k1));
      opponentList.push(createObject("机房位置", data.k2));
      opponentList.push(createObject("机架数量", data.k3));
      opponentList.push(createObject("机架功耗", data.k4));
      opponentList.push(createObject("资费", data.k5));
      opponentList.push(createObject("合同起始时间", data.k6));
      opponentList.push(createObject("合同终止时间", data.k7));
      opponentList.push(createObject("总收入（年）", data.k8));
    }else if(busType == "专线"){
      opponentList.push(createObject("业务类型", data.k1));
      opponentList.push(createObject("带宽", data.k2));
      opponentList.push(createObject("专线条数", data.k3));
      opponentList.push(createObject("资费", data.k4));
      opponentList.push(createObject("合同起始时间", data.k5));
      opponentList.push(createObject("合同终止时间", data.k6));
      opponentList.push(createObject("总收入（年）", data.k7));
      opponentList.push(createObject("语音专线的日均话务量", data.k8));    
    }else if(busType == "集团电话"){
      opponentList.push(createObject("接入方式", data.k1));
      opponentList.push(createObject("装机量", data.k2));
      opponentList.push(createObject("装机地址", data.k3));      
      opponentList.push(createObject("话务号段", data.k4));
      opponentList.push(createObject("资费", data.k5));
      opponentList.push(createObject("合同起始时间", data.k6));
      opponentList.push(createObject("合同终止时间", data.k7));
      opponentList.push(createObject("总收入（年）", data.k8));
    }else if(busType == "物联网"){
      opponentList.push(createObject("网络类型", data.k1));
      opponentList.push(createObject("业务类型", data.k2));
      opponentList.push(createObject("应用场景", data.k3));
      opponentList.push(createObject("资费套餐", data.k4));
      opponentList.push(createObject("机架数量", data.k5));
      opponentList.push(createObject("资费", data.k6));
      opponentList.push(createObject("合同起始时间", data.k7));
      opponentList.push(createObject("合同终止时间", data.k8));
      opponentList.push(createObject("总收入（年）", data.k9));
    }
    
    var $tpl = $('#amz-detail');
    var source = $tpl.text();
    var template = Handlebars.compile(source);
    var myData = {
      list: opponentList,
      "error": "没有集团信息"
    };

    $("#opponent-list").children('li').remove();
    $("#opponent-list").children('div').remove();

    var html = template(myData);
    $tpl.before(html);
  };


  var loadOpponentData = function() {
    id = getParamValue("id");
    console.log("id="+id);
    $.ajax({
      type: "post",
      contentType: "application/json",
      url: "../../../opponent/getIdcDetailsById?id=" + id,
      dataType: "json",
      async: true,
      success: function(data) {
        console.log("data = " + data);
        pageData = data.resultCode;
        console.info(data.resultCode);
        if (data.resultCode != 1000) {
          console.log("获取RPC接口返回失败！code:" + data.message);
          return;
        }
        $("#modal-loading").modal('close');
        // 编辑页面传入ID
        //idcData = JSON.stringify(data.data);
        idcData = data.data.id;
        showOpponentData(data.data);
      },
      error: function(textStatus) {
        $("#modal-loading").modal('close');
        showBottomMsg("获取信息失败！");
        console.info("request error:");
      }
    });
  };

  var deleteSuccess = function(){
    params = getMyParams();
    if(params.indexOf("&id=")!=-1){
      params = params.substring(0, params.indexOf("&id="));
    }
    window.location.href = "opponent_list.html" + params;
  }

  var deleteOpponentData = function() {
    $("#modal-loading").modal('open');
    id = getParamValue("id");
    console.log("id="+id);
    $.ajax({
      type: "post",
      contentType: "application/json",
      url: "../../../opponent/deleteOpponent?id=" + id,
      dataType: "json",
      async: true,
      success: function(data) {
        showLoadingBar($("#loadingbar"), false);
        console.log("data = " + data);
        pageData = data.resultCode;
        console.info(data.resultCode);
        if (data.resultCode != 1000) {
          console.log("获取RPC接口返回失败！code:" + data.message);
          return;
        }
        $("#modal_content").html("删除成功！");
        setTimeout(deleteSuccess,2000);
      },
      error: function(textStatus) {
        $("#modal-loading").modal('close');
        console.info("request error:");
        alert("删除失败，请联系管理员！");
      }
    });
  };

  $(function() {
    /*滚动条*/
    var $doc = $(document);
    $doc.scroll(function() {
      console.log("height = " + ($doc.height() - $(window).height()));
      console.log("scrollTop = " + Math.ceil($doc.scrollTop()));
      console.log("Math.ceil($doc.scrollTop()) = " + Math.ceil($doc.scrollTop()));
      if (Math.ceil($doc.scrollTop()) >= ($doc.height() - $(window).height())) {
        var $msg = $("#no-data-msg");
        console.log("debug 1");
        if (!$.isEmptyObject(pageData)) {
          console.log("debug 2");
          if (!$msg.hasClass("am-hide")) {
            console.log("debug 3");
            $msg.addClass("am-hide");
          }
          console.log("pageNumber=" + pageNumber);
          pageNumber++;
          loadGroupData(pageNumber);
        } else {
          console.log("debug 5");
          if ($msg.hasClass("am-hide")) {
            console.log("debug 6");
            $msg.removeClass("am-hide");
          }
        }
      }
    });


    $("#search_btn").on("click", function() {
      operator = $("#searchType").val();
      pageNumber=0;
      opponentList=[];
      loadOpponentData(pageNumber, operator);
    });
    $("#search").on("click", function() {
      $('#search-modal').modal('open');
    });
    $("#delete_btn").on('click', function(){
      $('#delete-confirm-modal').modal('open');
    });
    $("#edit_btn").on('click', function(){
    	window.location.href = "idc_edit.html?" + "id=" + idcData;
    });
    $("#delete_confirm_btn").on("click", function() {      
      deleteOpponentData();
    });
    $("#modal-loading").modal('open');
    loadOpponentData(pageNumber, "");
  });
})(jQuery);